/*
* 数据库的基本操作：
* 增、删、改、查
*/
class Basic {
    constructor(sequelize) {
        this.sequelize = sequelize
    }
    // *查询所有数据
    async findAllByModel (model, options = {}) {
        await this.sequelize.sync()
        let result = await model.findAll(options)
        // 对result数据进行处理
        let list = []
        for (let i of result) {
            list.push(this.formatDbByFind(i.toJSON()))
        }
        return list
    }
    // *查询一条数据
    async findOneByModel (model, conditions = {}) {
        await this.sequelize.sync()
        const result = await model.findOne(conditions)
        let obj = result ? this.formatDbByFind(result.toJSON()) : null
        return obj
    }
    // *搜索特定元素或创建它
    async findOrCreateByModel (model, options = {}) {
        try {
            await this.sequelize.sync()
            const result = await model.findOrCreate(options)
            let obj = {}
            if (Array.isArray(result) && result.length > 0) {
                obj = this.formatDbByFind(result[0].toJSON())
            }
            return obj
        } catch (e) {
            throw e
        }
    }
    // 在数据库中搜索多个元素
    async findAndCountAllByModel (model, options = {}) {
        await this.sequelize.sync()
        const result = await model.findAndCountAll(options)
        let obj = {
            count: result.count, // 满足匹配条件的总数
            rows: []
        }
        if (result.rows && result.rows.length) {
            for (let i of result.rows) {
                obj.rows.push(this.formatDbByFind(i.toJSON()))
            }
        }
        return obj
    }
    // 针对数据查询的结果标准输出
    formatDbByFind (jsonValue) {
        const value = Object.assign({}, jsonValue)
        let result = {}
        for (let key in value) {
            result[key] = value[key]
        }
        return result
    }
    // 新增一条数据
    async createByModel (model, params) {
        try {
            await this.sequelize.sync() // 同步所有尚未在数据库中的模型
            await model.create(params)
        } catch (e) {
            throw e
        }
    }
    // 批量增加
    async bulkCreateByModel (model, params = []) {
        try {
            await this.sequelize.sync() // 同步所有尚未在数据库中的模型
            await model.bulkCreate(params)
        } catch (e) {
            throw e
        }
    }
    // 清空当前的模型（即清空当前user表）
    async dropByModel (model) {
        try {
            await this.sequelize.sync() // 同步所有尚未在数据库中的模型
            await model.drop()
        } catch (e) {
            throw e
        }
    }
    // 删除所有的匹配项
    async destroyByModel (model, conditions) {
        try {
            await this.sequelize.sync() // 同步所有尚未在数据库中的模型
            await model.destroy(conditions)
        } catch (e) {
            throw e
        }
    }
    // 更新所有满足条件的选项
    async updateByModel (model, params, conditions) {
        try {
            await this.sequelize.sync() // 同步所有尚未在数据库中的模型
            await model.update(params, conditions)
        } catch (e) {
            throw e
        }
    }
}

module.exports = Basic
